﻿using System;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using CXMLStudioIDE.DataAccessLayer;
using System.Data;
using CXMLStudioIDE.Common;
using System.Diagnostics;

namespace CXMLStudioIDE.Common.EntityProperties
{
    [Serializable()]
    public class CXMLPropertiesUtil
    {
        public static EntityProperty GetEntityPropertyFromProperty(object ctrl, PropertyDescriptor myProperty)
        {
            EntityProperty EP = null;
            try
            {

                //EP.ValueType = myProperty.PropertyType.FullName;
                //EP.PropertyGroup = myProperty.Category;
                Type T = myProperty.GetValue(ctrl).GetType();
                switch (myProperty.PropertyType.Name)
                {
                    case "Color":
                        T = myProperty.GetValue(ctrl).GetType();
                        System.Drawing.Color objT = (System.Drawing.Color)Activator.CreateInstance(T);
                        objT = (System.Drawing.Color)myProperty.GetValue(ctrl);
                        EP = new EntityProperty(myProperty.Name, objT.ToArgb(), T, false, true, myProperty.Category, "", null);
                        break;

                    case "ControlBindingsCollection":
                        DataBinding db = new DataBinding();
                        MyBinding mb = null;

                        ControlBindingsCollection CBC = (ControlBindingsCollection)myProperty.GetValue(ctrl);
                        List<MyBinding> bindings = new List<MyBinding>();
                        foreach (Binding b in CBC)
                        {
                            mb = new MyBinding();
                            mb.DataMember = b.BindingMemberInfo.BindingMember;
                            mb.DataSource = b.DataSource.ToString();
                            mb.PropertyName = b.PropertyName;
                            bindings.Add(mb);
                        }
                        db.Bindings = bindings;
                        EP = new EntityProperty(myProperty.Name, null, typeof(ControlBindingsCollection), false, true, myProperty.Category, "", null);
                        EP.DataBinding = db;
                        break;

                    default:

                        if (myProperty.PropertyType.IsSerializable && myProperty.Name != "" && myProperty.Name != "\\")
                        {
                            if (myProperty.GetValue(ctrl) != null)
                            {
                                object o = ControlFactory.GetValueObjectFromString(T, myProperty.GetValue(ctrl).ToString(), null);
                                EP = new EntityProperty(myProperty.Name, o, myProperty.GetValue(ctrl).GetType(), false, true, myProperty.Category, "", null);
                            }
                        }
                        if (!myProperty.PropertyType.IsSerializable)
                        {
                            if (myProperty.GetValue(ctrl) != null)
                            {
                                object o = ControlFactory.GetValueObjectFromString(T, myProperty.GetValue(ctrl).ToString(), null);
                                EP = new EntityProperty(myProperty.Name, o, myProperty.GetValue(ctrl).GetType(), false, true, myProperty.Category, "", null);
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
                //do nothing, just continue
            }
            return EP;
        }

        public static PropertySet GetPropertySetFromControl(object ctrl, string sZ_EntityName, DisplayContext context)
        {
            PropertyDescriptorCollection properties = null;
            PropertySet pSet = new PropertySet(sZ_EntityName);
            pSet.DisplayContext = context;
            properties = TypeDescriptor.GetProperties(ctrl);
            EntityProperty EP = null;

            foreach (PropertyDescriptor myProperty in properties)
            {
                EP = GetEntityPropertyFromProperty(ctrl, myProperty);
                if (EP != null)
                {
                    pSet.Add(EP);
                }
            }
            if (pSet.Count > 0)
            {
                if (!ctrl.GetType().Name.Equals("CXMLTreeNode"))
                {
                    EP = new EntityProperty("Control", ctrl.GetType().Name, ctrl.GetType(), false, true, "Misc", "", null);
                    pSet.Add(EP);
                }
            }
            return pSet;
        }

    }
}
